<?php

namespace app\api\controller;

use app\api\model\ChickenCards as ChickenCards;
use Illuminate\Support\Carbon;
use support\Request;

class ChickenCardsController
{


    public function getHenInfo(Request $request){
        $res = ChickenCards::where('id',2)->first();
        return json([
            'code' => 2,
            'msg' => '获取成功！',
            'data' => $res,
        ]);

    }
    /**
     * @param Request $request
     * @param ChickenCards $DB
     * @return \support\Response
     * 添加母鸡卡模板
     */
    public function add(Request $request, ChickenCards $DB)
    {
        $id = $request->post('id');
        $req = $request->only(['green_card_1', 'green_card_2', 'green_card_3', 'green_card_4', 'green_card_5', 'blue_card_1', 'blue_card_2', 'gold_card_1',
            'red_card_1','black_card_1','is_sold_out','green_card_1_draw_rate','green_card_2_draw_rate','green_card_3_draw_rate','green_card_4_draw_rate','green_card_5_draw_rate',
            'blue_card_1_draw_rate','blue_card_2_draw_rate','gold_card_1_draw_rate','red_card_1_draw_rate','black_card_1_draw_rate','eggs_per_card','eggs_directly_get',
            'return_fund_percentage']);
        $res = $DB->first();
        $req['created_at'] = Carbon::now()->toDateTimeString();
        if (!$res) {
            $res = $DB->insertGetId($req);
            $code = 2;
            $msg = '添加成功!';
        } else {
            $code = 0;
            $msg = '模板已存在，请修改';
        }
        return json([
            'code' => $code,
            'msg' => $msg,
            'data' => $res,
        ]);
    }

    /**
     * @param Request $request
     * @param ChickenCards $DB
     * @return \support\Response
     * 修改信息
     */
    public function update(Request $request, ChickenCards $DB)
    {
        $req = $request->only(['id','green_card_1', 'green_card_2', 'green_card_3', 'green_card_4', 'green_card_5', 'blue_card_1', 'blue_card_2', 'gold_card_1',
            'red_card_1','black_card_1','is_sold_out','green_card_1_draw_rate','green_card_2_draw_rate','green_card_3_draw_rate','green_card_4_draw_rate','green_card_5_draw_rate',
            'blue_card_1_draw_rate','blue_card_2_draw_rate','gold_card_1_draw_rate','red_card_1_draw_rate','black_card_1_draw_rate','eggs_per_card','eggs_directly_get',
            'return_fund_percentage']);
        $req['updated_at'] = Carbon::now()->toDateTimeString();
        $res = $DB->where('id', $req['id'])->update($req);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }

}
